#set($layout = "blank.html")
<div class="space-4"></div>
<div class="row">
	<div class="col-xs-1"></div>
	<div class="col-xs-10">
		<form id="editForm">
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 权限名: </label>
					<span  class="col-sm-3 control-label no-padding-right" for="form-field-1">$!{obj.data.permission.name}</span>	
			</div>
			<div class="space-4"></div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 是否是菜单: </label>
					<select class="form-control" name="isMenu" onchange="showUrl(this.value)">
					<option value="-99">
						请选择是否是菜单
                    </option>
					<option value="true" #if($!{obj.data.permission.isMenu()})selected #end>是 </option>
					<option value="false"#if(!$!{obj.data.permission.isMenu()})selected #end>否 </option>	
                </select>	
			</div>
			<div class="space-4"></div>
			<div class="form-group"#if(!$!{obj.data.permission.isMenu()}) style="display:none"  #end id="urlDiv">
			<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 权限url </label>
			<div class="col-sm-9">
				<input type="text" name="url" class="col-xs-8 col-sm-5" placeholder="请输入权限url "  value="$!{obj.data.permission.url}" data-origin="$!{obj.data.permission.url}" onblur="checkUrl(this.value)"/>
			</div>
			</div>
		<div class="space-20"></div>
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 权限路径分组</label>
			<div class="col-sm-5">
				<select class="form-control" name="menuGroup">
					<option value="-99">
						请选择权限路径分组
                    </option>
					<optgroup label="---------------------">
						#foreach($permissionGroup in ${obj.data.permissionGroup})
							<option value="$!{permissionGroup}" #if($!{obj.data.permission.menuGroup}==$!{permissionGroup})  selected #end> $!{permissionGroup.name} </option>
						#end
                    </optgroup>
                </select>
			</div>
		</div>
		<div class="space-4"></div>
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right" for="form-field-1">菜单高亮关键字<small class="text-info">例如:user_list</small></label>
			<div class="col-sm-9">
				<input type="text" name="hilightKey" class="col-xs-8 col-sm-5" placeholder="请输入菜单高亮关键字"/>
			</div>
		</div>
		<div class="space-20"></div>
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right" for="form-field-1">描述 </label>
			<div class="col-sm-9">
				<input type="text" name="description" class="col-xs-10 col-sm-5" placeholder="描述 " value="$!{obj.data.permission.description}" />
			</div>
		</div>
			<div class="space-12"></div>
			<input type="hidden" name="id" value="$!{obj.data.permission.id}"/>
			<div class="pull-right" style="margin-right:45px">
				<button type="button" class="btn btn-sm btn-primary" onclick="save()">
					<i class="fa fa-save"></i> 提交
				</button>
				<button class="btn btn-sm btn-warning btn-dialog-undo">
					<i class="fa fa-undo"></i> 返回
				</button>
			</div>
		</form>
	</div>
	<div class="col-xs-1"></div>
</div>
<script type="text/javascript">
function save(){
	var flag=$('input').validation();
	var url=$('input[name=url]').val();
	var originUrl=$('input[name=url]').data("origin");
	var isMenu = $('select[name=isMenu]').val();
	console.log(originUrl);
	if((url!=originUrl||url=="")&&isMenu=="true"){
		if(!checkUrl(url)){
			return;
		}
	}
	if($('select[name=menuGroup]').val()==-99){
		showMessage("请选择权限路径分组");
		return;
	}
	if(flag){
	$.post(getRootPath() + '/permission/update', $('#editForm').serialize(), commonAjaxCallBasck, 'json');
	}
}

function checkUrl(url){
		console.log(url);
		if(url==null||url==""){
			showMessage("权限url不能为空");
			return false;
		}
		var r;
		$.ajax({ 
		url:getRootPath() + '/permission/checkUrl',
		async:false,
		type:"post",
		dataType:"json",
		data:{
			url:url
		},
		  success: function(result){
      	 if(result.operationState == "SUCCESS"){
      	 		r=true;
      	 }else{
      	 	r=false;
      	 	showMessage(result.data.reason);
      	 }
      }
      });
        return r;	
	}
	function showUrl(value){
	if(value=="true"){
		$("#urlDiv").show();
	}else{
		$("#urlDiv").hide();
		$("input[name=url]").val("");
	}
	}
</script>
